package com.sharp.authorization.dao.common.repository;

import com.github.pagehelper.PageInfo;
import com.sharp.authorization.dao.common.entity.AcUser;
import com.sharp.authorization.dao.common.mapper.AcUserDao;
import com.sharp.authorization.vo.user.QueryUserReq;
import com.sharp.kernel.constant.db.SqlConstant;
import com.sharp.kernel.mybatis.repository.BaseMysqlRepository;
import com.sharp.kernel.utils.DbUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Title: AcUserService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2022/6/14 17:46
 */
@Service
public class AcUserService extends BaseMysqlRepository<AcUser, AcUserDao> {

    @Resource
    private AcUserDao acUserDao;

    public AcUser getUserByEmail(String email) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("email", email);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByPhoneNumber(String phoneNumber) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("phone", phoneNumber);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByGuid(String guid) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("guid", guid);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByIdCardNumber(String idCardNum) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("idCardNum", idCardNum);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByAppKey(String appKey) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("appKey", appKey);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByWechatId(String wechatId) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("wechatId", wechatId);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public AcUser getUserByLoginId(String loginId) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("loginId", loginId);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }

    public PageInfo<AcUser> getUserByQueryUserReq(QueryUserReq req) {
        Map<String, Object> propertiesMap = new HashMap<>(6);

        if (StringUtils.isNotBlank(req.getLoginId())) {
            propertiesMap.put("loginId", req.getLoginId());
        }
        if (StringUtils.isNotBlank(req.getName())) {
            propertiesMap.put("name", req.getName());
        }
        if (StringUtils.isNotBlank(req.getPhone())) {
            propertiesMap.put("phone", req.getPhone());
        }
        if (StringUtils.isNotBlank(req.getEmail())) {
            propertiesMap.put("email", req.getEmail());
        }
        if (StringUtils.isNotBlank(req.getStatus())) {
            propertiesMap.put("status", req.getStatus());
        }
        propertiesMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        Example example = super.getExample(propertiesMap);
        PageInfo<AcUser> pageInfo = DbUtil.buildPageInfo(req, AcUser.class);
        return super.select(example, pageInfo);
    }

    public void deleteByUserId(String loginId) {
        if (StringUtils.isNotBlank(loginId)) {
            Example example = super.getExample();
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("loginId", loginId);
            super.delete(example);
        }
    }

    public AcUser getUserById(String userId) {
        return super.selectByPrimaryKey(userId);
    }

    public AcUser getUserByCustomerNumber(String customerNumber) {
        Map<String, Object> propertyMap = new HashMap<>(1);
        propertyMap.put("customerNumber", customerNumber);
        propertyMap.put(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return selectOne(propertyMap);
    }
}


